package com.lc.servlet;

import com.lc.bean.Menu;
import com.lc.bean.Role;
import com.lc.bean.Users;
import com.lc.service.MenuService;
import com.lc.service.RoleService;
import com.lc.service.UsersService;
import com.lc.service.impl.MenuServiceImpl;
import com.lc.service.impl.RoleServiceImpl;
import com.lc.service.impl.UsersServiceImpl;
import com.lc.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @description:用于进行角色查询等功能的操作
 * @author: ManolinCoder
 * @time: 2021/8/24
 */
@WebServlet("/power/role/roles")
public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)){
            select(req,resp);
        }else if ("selectmenus".equals(method)){
            selectmenus(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("del".equals(method)){
            del(req,resp);
        }
    }

    //查询数据(分页)
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数(分页参数+模糊查询参数)
        String index = req.getParameter("index");
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法、2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Role> roleList = roleService.getRoleList(pageIndex, pageUtil.getPageSize());
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(roleList);

        //3.存值并跳转页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }

    //查询菜单列表
    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调取service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }

    //新增角色权限
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String rolename = req.getParameter("rolename");
        String[] menuids = req.getParameterValues("menuid");
        String state = req.getParameter("state");
        //调取service方法
        int i = roleService.insertRole(rolename, state, menuids);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        //跳转页面
        if (i>0){
            writer.println("<script>alert('新增成功');location.href='/power/role/roles?method=select';</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/power/role/roles?method=selectmenus';</script>");
        }
    }

    //更新角色信息及权限
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String rolename = req.getParameter("rolename");
        String[] menuids = req.getParameterValues("menuid");
        String state = req.getParameter("state");
        String roleid = req.getParameter("roleid");

        //修改角色表信息
        Role role = new Role();
        role.setRoleName(rolename);
        role.setRoleId(Integer.parseInt(roleid));
        role.setRoleState(Integer.parseInt(state));
        int i = roleService.updateRole(role);

        //删除原来角色所对应的菜单列表(如果该角色没有权限则会返回0)
        int i1 = roleService.delRoleMenu(Integer.parseInt(roleid));
        //新增修改后的角色的权限菜单列表
        int i2 = roleService.insertRole(Integer.parseInt(roleid), menuids);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        //跳转页面
        if (i>0 && i1>0 && i2>0){
            //这里不能直接跳转页面否则页面中会没有数据，应当跳转相应的servlet
            writer.println("<script>alert('更新成功');location.href='/power/role/roles?method=select';</script>");
        }else {
            writer.println("<script>alert('更新失败');location.href='/power/role/getRoleAndMenu?roleId="+roleid+"';</script>");
        }
    }

    //删除角色(先删除middle表信息，再删除role表信息)
    protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据
        String roleId = req.getParameter("roleId");

        //调用service方法
        //先删除middle表信息
        int i = roleService.delRoleMenu(Integer.parseInt(roleId));
        //再删除role表信息
        int i1 = roleService.delRole(Integer.parseInt(roleId));

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        //跳转页面
        if (i>0 && i1>0){
            //这里不能直接跳转页面否则页面中会没有数据，应当跳转相应的servlet
            writer.println("<script>alert('删除成功');location.href='/power/role/roles?method=select';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power/role/roles?method=select';</script>");
        }
    }
}
